local yingying = fk.CreateSkill{
  name = "xiaobai__yingying",
}

Fk:loadTranslationTable{
  ["xiaobai__yingying"] = "营盈",
  [":xiaobai__yingying"] = "你可以将手牌数调整为手牌上限，视为使用或打出【杀】或【闪】。若你因“营盈”累计的摸牌数大于弃牌数，则你只能通过弃牌发动“营盈”。",
  ["@@xiaobai__yingying_cantDraw"] = "营盈 仅弃牌"
}

yingying:addEffect("viewas",{
  pattern = "slash,jink",
  card_filter = function (self,player, to_select, selected)
    local handCardNum = player:getHandcardNum()
    local maxCardNum = player:getMaxCards()
    if handCardNum > maxCardNum then return table.contains(player:getCardIds("h"), to_select) and #selected < (handCardNum - maxCardNum)
    else return false end
  end,
  interaction = function(self, player)
    local names = {}
    for _, name in ipairs({"slash","jink"}) do
      local card = Fk:cloneCard(name)
      if (Fk.currentResponsePattern == nil and player:canUse(card)) or
        (Fk.currentResponsePattern and Exppattern:Parse(Fk.currentResponsePattern):match(card)) then
        table.insertIfNeed(names, card.name)
      end
    end
    if #names == 0 then return end
    return UI.CardNameBox {choices = names}
  end,
  view_as = function (self, player, cards)
    if not self.interaction.data then return end
    local card = Fk:cloneCard(self.interaction.data)
    card.skillName = yingying.name
    local handCardNum = player:getHandcardNum()
    local maxCardNum = player:getMaxCards()
    if #cards == handCardNum - maxCardNum then
      card:addFakeSubcards(cards)
    elseif handCardNum - maxCardNum > 0 then
      return nil
    end
    return card
  end,
  before_use = function (self, player, use)
    local room = player.room
    local difference = player:getHandcardNum() - player:getMaxCards()
    if difference > 0 then
      room:throwCard(use.card.fake_subcards, yingying.name, player)
      room:addPlayerMark(player, "xiaobai__yingying_discard", #use.card.fake_subcards)
      if player:getMark("xiaobai__yingying_discard") >= player:getMark("xiaobai__yingying_draw") then
        room:removePlayerMark(player, "@@xiaobai__yingying_cantDraw")
      end
    else
      player:drawCards(-difference, yingying.name)
      room:addPlayerMark(player, "xiaobai__yingying_draw", -difference)
      if player:getMark("xiaobai__yingying_discard") < player:getMark("xiaobai__yingying_draw") then
        room:addPlayerMark(player, "@@xiaobai__yingying_cantDraw")
      end
    end
  end,
  enabled_at_play = function (self, player)
    if player:getHandcardNum() < player:getMaxCards() then
      return player:getMark("@@xiaobai__yingying_cantDraw") == 0
    else
      return player:getHandcardNum() > player:getMaxCards()
    end
  end,
  enabled_at_response = function (self, player, response)
    if player:getHandcardNum() < player:getMaxCards() then
      return player:getMark("@@xiaobai__yingying_cantDraw") == 0
    else
      return player:getHandcardNum() > player:getMaxCards()
    end
  end
})

return yingying